<!-- 理化检测任务单详情(委托型) -->
<template>
    <div class="dialog-container" id="dialog-container">
        <el-dialog v-dialogDrag :fullscreen="isFullScreen" :custom-class="isFullScreen?'fullscreen-default-dialog':'vertical-center-dialog'" append-to-body :close-on-click-modal="false" :visible.sync="dialogVisible" @close="$emit('update:show', false)" :before-close="handleClose" :show="show" width="800px">
            <div slot="title">
                <span style="color:white;">报告审核</span>
                <button type="button" aria-label="fullScreen" class="el-dialog__headerbtn" style="margin-right:25px;" @click="isFullScreen = !isFullScreen">
                    <i class="el-dialog__close iconfont icon-fuzhicopy22"></i>
                </button>
            </div>
            <div class="uploadTitle">
                <img src="../../../../../assets/images/sagw.png" style="float:left;">
                <h2 style="text-align:center;">理化检测报告</h2>
                <span style="float: right;">委托单号：{{form.taskNo}}</span>
            </div>
            <el-form :model="form" :inline="true" label-width="130px" size="small">
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="零件号：">
                            <el-input v-model="form.partNumber" disabled></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="热处理炉号：">
                            <el-input v-model="form.heatingNumber" disabled></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="任务类型：">
                             {{getType(form.taskType)}}
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="零件名称：">
                            <el-input v-model="form.partName" disabled></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="批次零件总数量：">
                            <el-input v-model="form.totalPartNo" disabled></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="委托方：">
                            <el-input v-model="form.applyUser.name" disabled></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="零件图号：">
                            <el-input v-model="form.partGraphNumber" disabled></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="材料牌号：">
                            <el-input v-model="form.materialNo" disabled></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="委托日期：">
                            <el-date-picker v-model="form.applyTime" type="date" disabled style="width: 200px;"></el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="图纸版本：">
                            <el-input v-model='form.part.graphVersion' disabled></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="原材料炉号：">
                            <el-input v-model="form.materialFurnanceNumber" disabled></el-input>
                        </el-form-item>
                    </el-col>
                    <!-- <el-col :span="12">
            <el-form-item label="委托单号：">
              <el-input v-model='form.taskNo'></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row> -->
                    <el-col :span="12" v-if="form.taskType==1">

                        <el-form-item label="需求完成日期：">
                            <el-input v-model="form.expectCompleteTime" disabled></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="20">
                        <el-form-item label="样品信息：">
                            {{this.sampleStatusName}}共 {{form.quantity}} 件
                            <!-- <el-radio-group v-model="form.sampleStatus" disabled>
                                <el-radio label="实物">实物
                                    <el-input v-if="form.sampleStatus == '实物'" v-model="form.quantity" disabled style="width: 70px;margin: 0 5px;"></el-input>件</el-radio>
                                <el-radio label="试样">试样
                                    <el-input v-if="form.sampleStatus == '试样'" v-model="form.quantity" disabled style="width: 70px;margin: 0 5px;"></el-input>件</el-radio>
                            </el-radio-group> -->
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="24">
                        <el-form-item label="备注：">
                            <el-input type="textarea" v-model="form.remark" disabled  style="width:250px;"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row v-if="form.taskType==1">
                    <el-col :span="24">
                        <el-form-item label="任务目的与要求：">
                            <el-radio-group v-model="form.taskTarget" disabled>
                                <el-radio label="新产品试制">新产品试制</el-radio>
                                <el-radio label="常规均匀性实验">常规均匀性实验</el-radio>
                                <el-radio label="失效零件分析">失效零件分析</el-radio><br>
                                <el-radio label="复检">复检</el-radio>
                                <el-radio label="设备新增/大修验收">设备新增/大修验收</el-radio>
                                <el-radio label="其他">其他</el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-tabs v-model="activeName" @tab-click="handleClick">
                    <el-tab-pane :label="item.title" :name="item.title" v-for="item in editableTabs" :key="item.key">
                        <el-table :data="tableData[item.key]">
                            <el-table-column type='index' label="编号" align="center" width="60"></el-table-column>
                            <el-table-column prop="taskField.fieldName" label="检测项目" align="center" width="100"></el-table-column>
                            <el-table-column prop="testPart" label="检测位置" align="center" width="100">
                                <template slot-scope="scope">
                                    {{getPosition(scope.row.taskField.testPart)}}
                                </template>
                            </el-table-column>
                            <!-- <el-table-column prop="taskField.fieldStandard" label="检测标准" align="center" width="150">

              </el-table-column>-->
                            <el-table-column prop="standardInfoStr" label="检测要求" align="center" width="180">

                            </el-table-column>
                            <el-table-column label="实测数据" align="center">
                                <el-table-column prop="actData1" label="1" align="center" width="90">
                                    <template slot-scope="scope">
                                        <div :class="unqualifiedColColor(scope.row, 1)==0 ? 'div-red': 'div-normal'">
                                          {{getData(scope.row,1)}}
                                        </div>
                                    </template>
                                </el-table-column>
                                <el-table-column prop="actData2" label="2" align="center" width="90">
                                    <template slot-scope="scope">
                                       <div :class="unqualifiedColColor(scope.row, 1)==0 ? 'div-red': 'div-normal'">
                                          {{getData(scope.row,2)}}
                                        </div>
                                    </template>
                                </el-table-column>
                                <el-table-column prop="actData3" label="3" align="center" width="90">
                                    <template slot-scope="scope">
                                       <div :class="unqualifiedColColor(scope.row, 1)==0 ? 'div-red': 'div-normal'">
                                          {{getData(scope.row,3)}}
                                        </div>
                                    </template>
                                </el-table-column>
                                <el-table-column prop="actData4" label="4" align="center" width="90">
                                    <template slot-scope="scope">
                                        <div :class="unqualifiedColColor(scope.row, 1)==0 ? 'div-red': 'div-normal'">
                                          {{getData(scope.row,4)}}
                                        </div>
                                    </template>
                                </el-table-column>
                                <el-table-column prop="actData5" label="5" align="center" width="90">
                                    <template slot-scope="scope">
                                       <div :class="unqualifiedColColor(scope.row, 1)==0 ? 'div-red': 'div-normal'">
                                          {{getData(scope.row,5)}}
                                        </div>
                                    </template>
                                </el-table-column>
                            </el-table-column>
                            <el-table-column prop="device" label="检测设备" align="center" width="200">
                                <template slot-scope="scope">
                                    <el-select v-model='scope.row.deviceId' disabled>
                                        <el-option v-for="item in scope.row.taskField.deviceList" :key="item.id" :label="item.codeAndName" :value="item.id"></el-option>
                                    </el-select>
                                </template>
                            </el-table-column>
                            <el-table-column prop="remark" label="备注" align="center" width="200">
                                <template slot-scope="scope">
                                  {{scope.row.testResult == 1 ? 'PASS,': 'NOT PASS,'}}  {{scope.row.remark}}
                                </template>
                            </el-table-column>
                            <el-table-column prop="remark" label="附件" align="center" width="200">
                                <template slot-scope="scope">
                                    <a href="javascript:void(0)" @click='downloadResultAttach(scope.row.id)'>{{scope.row.fileName}}</a>


                                </template>
                            </el-table-column>
                        </el-table>
                    </el-tab-pane>

                </el-tabs>
                <el-row style="margin-top: 10px;">
                    <el-col :span="16">
                        <!-- <el-form-item label="上传附件："> -->
                        <el-upload class="upload-demo" ref="upload" :action="uploadUrl" :on-preview="handlePreview" :on-remove="handleRemove" :file-list="fileList" :before-remove='cancelRemove' :data="uploadData" :on-success="uploadSuccess" :auto-upload="false">
                            <input type='hidden' name='taskId' v-model='detailId'></input>
                            <!-- <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
                <el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">上传到服务器</el-button> -->
                        </el-upload>
                        <!-- </el-form-item> -->
                    </el-col>
                    <!-- <el-col :span="4">
            <uploading-attachments-button></uploading-attachments-button>
          </el-col> -->
                </el-row>

                <el-card shadow="never">
                    <el-form-item label="报告结论：">
                        <!-- <el-radio-group v-model="form.testResult" disabled>
              <el-radio :label="1">符合</el-radio>
              <el-radio :label="0">不符合</el-radio>
            </el-radio-group> -->
                        {{reportResult}}
                    </el-form-item>
                </el-card>

                <hr>
                <div v-if='form.taskStatus==7'>
                    <el-row>
                        <el-col :span="22">
                            <el-form-item label="审核情况：" prop="result">
                                <el-radio-group v-model="form.auditResult" disabled>
                                    <el-radio :label="1">通过</el-radio>
                                    <el-radio :label="0">不通过</el-radio>
                                </el-radio-group>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="24">
                            <el-form-item label="审核理由：" prop="reason">
                                <el-input type="textarea" v-model="form.auditReason" style="width: 568px;" disabled></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    
                    <el-row>
                        <el-col :span="12" :offset="12">
                            <el-form-item label="审核人：">
                                <el-input v-model="form.auditUser.name" disabled></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </div>

                <el-row>
                    <el-col :span="22">
                        <el-form-item label="审核情况：" prop="result" >
                            <el-radio-group v-model="audit.result" @change="setIsPush()">  
                                <el-radio :label="1">通过</el-radio>
                                <el-radio :label="0">不通过</el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="24">
                        <el-form-item label="审核理由：" prop="reason">
                            <el-input type="textarea" v-model="audit.reason" style="width: 568px;"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row v-if='form.taskStatus==7 && audit.result==0 && form.taskType==4'>
                        <el-col :span="22">
                            <el-form-item label="是否推送：" prop="isPush">
                                <el-radio-group v-model="audit.isPush">
                                    <el-radio :label="1">是</el-radio>
                                    <el-radio :label="0">否</el-radio>
                                </el-radio-group>
                            </el-form-item>
                        </el-col>
                    </el-row>
                <el-row>
                    <el-col :span="12" :offset="12">
                        <el-form-item label="审核人：">
                            <el-input v-model="audit.approver" disabled></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>

            </el-form>



            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="auditReport" v-if='form.taskStatus==6||form.taskStatus==7'>提 交</el-button>
                <el-button @click="dialogVisible = false">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import UploadingAttachments from "dialog/experimental-management/uploading-attachments-button";
    import {
        getApplyInfoByIdService,
        getPositionList,
        getTypeList,
        postDeleteAttach,
        getSampleStatusDrop
    } from "service/task";
    import {
        getLableFromList
    } from "utils/common";
    import {
        
         APPLY_TYPE_LIST,
        APPLY_STATUS_LIST,
        RESULT_LIST
    } from 'service/constant'
    import {
        postSaveReportService,
        postReportAuditService,
        postReportSecondAuditService
    } from "../../../../../service/task";

    export default {
        components: {
            "uploading-attachments-button": UploadingAttachments
        },
        data() {
            return {
                sampleStatusName:"",
                positionList: [],
                reportResult: '',
                isFullScreen: false,
                dialogVisible: this.show,
                activeName: "样品1",
                uploadUrl: `${process.env.API_URL}task/uploadAttach`,
                form: {
                    partNumber: "",
                    heatingNumber: "",
                    taskType: "",
                    partName: "",
                    totalPartNo: "",
                    applyUser: "",
                    partGraphNumber: "",
                    materialNo: "",
                    applyTime: "",
                    materialFurnanceNumber: "",
                    expectCompleteTime: "",
                    sampleStatus: "",
                    quantity: "",
                    remark: "",
                    part: {},
                    auditUser: {},
                },
                audit: {
                    result: 1,
                    isPush: 1,
                    reason: '',
                    approver: JSON.parse(localStorage.getItem("currentUser"))['name']
                },
                editableTabs: [],
                tableData: [],
                recordData: [],
                fileList: [],
                uploadData: {
                    taskId: this.detailId
                }
            };
        },
        props: {
            show: {
                type: Boolean,
                default: false
            },
            detailId: {
                type: String
            }
        },
        mounted() {
            getPositionList().then(res => {
                this.positionList = res.data.result;
            });
        },
        watch: {

            show() {
                if (!this.dialogVisible) {
                    this.editableTabs = [];
                    this.tableData = [];
                }
                this.audit.result =1;
                this.audit.reason = "";
                this.dialogVisible = this.show;
                this.uploadData.taskId = this.detailId;
                if (this.dialogVisible) {
                    getApplyInfoByIdService(this.detailId).then(res => {
                        // let taskTarget = res.data.result.taskTarget.split(",");
                        // this.form.taskNo = ;
                        // this.form.applyTime = res.data.result.applyTime;
                        // this.form.taskType = res.data.result.taskType;
                        // this.form.applyOrgName = res.data.result.applyUser.org.name;
                        // this.form.applyUser = res.data.result.applyUser.name;
                        // this.form.expectCompleteTime = res.data.result.expectCompleteTime;
                        // this.form.projectNo = res.data.result.projectNo;
                        // this.form.partNumber = res.data.result.partNumber;
                        // this.form.partGraphNumber = res.data.result.partGraphNumber;
                        // this.form.partName = res.data.result.partName;
                        // this.form.materialNo = res.data.result.materialNo;
                        // this.form.totalPartNo = res.data.result.totalPartNo;
                        // this.form.sampleStatusPhase = res.data.result.sampleStatusPhase;
                        // this.form.materialFurnanceNumber =
                        //   res.data.result.materialFurnanceNumber;
                        // this.form.heatingNumber = res.data.result.heatingNumber;
                        // this.form.sampleStatus = res.data.result.sampleStatus;
                        // this.form.quantity = res.data.result.quantity;
                        // this.form.batchNumber = res.data.result.batchNumber;
                        // this.form.provider = res.data.result.provider;
                        // this.form.remark = res.data.result.remark;
                        this.form = res.data.result;
                        getSampleStatusDrop().then(res2 => {
                            for(let i=0; i<res2.data.result.length;i++){
                                if(res.data.result.sampleStatus==res2.data.result[i].code){
                                this.sampleStatusName=res2.data.result[i].name;
                                }
                            }
                        });
                        if (!this.form.applyUser) {
                            this.form.applyUser = {};
                        }
                        // this.form.taskTarget = taskTarget;
                        this.form.part = res.data.result.part ? res.data.result.part : {};
                        if (!this.form.auditUser) {
                            this.form.auditUser = {}
                        };

                        let resultData = res.data.result.taskResultList;
                        for (let i = 0; i < res.data.result.quantity; i++) {
                            this.tableData.push([]);
                            this.editableTabs.push({
                                title: `样品${i + 1}`,
                                key: i
                            });
                        }
                        for (let i = 0; i < resultData.length; i++) {
                            this.tableData[resultData[i].sampleNo].push(resultData[i]);
                              if(resultData[i].taskField.partField.unit!=null){
                            resultData[i].standardInfoStr = resultData[i].taskField.fieldStandard + '(' + resultData[i].taskField.standardInfo + ')'+resultData[i].taskField.partField.unit;
                            }else{
                            resultData[i].standardInfoStr = resultData[i].taskField.fieldStandard + '(' + resultData[i].taskField.standardInfo + ')';
                            } 
                        }
                        this.getReportResult();
                        let attachList = res.data.result.taskAttachList;
                        this.fileList = [];
                        attachList.forEach(e => {
                            let file = {
                                id: e.id,
                                name: e.fileName,
                                url: `${process.env.API_URL}task/downloadAttach?attachId=${e.id}`,
                                status: 'success',
                                uid: e.id,
                                percentage: 100,
                            };
                            this.fileList.push(file);
                        })
                    });
                }
            }
        },
        methods: {

              getType(t) {
        return getLableFromList(APPLY_TYPE_LIST, t);
      },             
      setIsPush() {
              let result = this.audit.result;
              if(result == 1) {
                  this.audit.isPush = 1;
              } else {
                  this.audit.isPush = "";
              }
             },
            unqualifiedColColor(row, col) {
                for (let i = 1; i <= 5; i++) {
                    let col1 = row.taskField.col1,
                        col2 = row.taskField.col2;
                    let data = row['actData' + i];
                    if (data && data != '') {
                        if (row.taskField.partField.operator == "range") {
                            if ((Number(data) < Number(col1) || Number(data) > Number(col2)) &&  i == col){
                                return 0;
                            }
                        } else if (row.taskField.partField.operator == "equal") {
                            if ((Number(data) != Number(col1)) && i == col) {
                                return 0;
                            }
                        } else if (row.taskField.partField.operator == "ge") {
                            if ((Number(data) < Number(col1)) && i == col) {
                                return 0;
                            }
                        } else if (row.taskField.partField.operator == "gr") {
                            if ((Number(data)<= Number(col1)) && i == col) {
                                return 0;
                            }
                        } else if (row.taskField.partField.operator == "le") {
                            if ((Number(data)> Number(col1)) && i == col) {
                                return 0;
                            }
                        } else if (row.taskField.partField.operator == "less") {
                            if ((Number(data)>= Number(col1)) && i == col) {
                                return 0;
                            }
                        } else {
                            if ((parseInt(data) != 1 ) && i == col) {
                                return 0;
                            }
                        }
                    }
                }
                return 1;
            },
            getReportResult() {
               this.reportResult = '';
               let finalReportResult = this.form.testResult == 1 ? '合格' : '不合格';
               if(this.editableTabs.length <=1) {
                 this.reportResult = this.reportResult + finalReportResult;
                 return;
               }
               for(let i = 0; i < this.editableTabs.length; i++) {
                   let title = this.editableTabs[i]['title'] + '结论';
                   let reportResult = '合格';
                   for(let j = 0; j < this.tableData.length; j++) {
                     let tableDataList = this.tableData[i];
                     let n = 0;
                     let r =tableDataList.length;
                     for(let m = 0; m < tableDataList.length; m++) {
                         let testResultStatus = tableDataList[m]['testResult'];
                         if(testResultStatus == 1) {
                            n++;
                         }
                     }
                    if(n == r) {
                         reportResult = '合格';
                         break;
                     } else {
                          reportResult = '不合格';
                         break;
                     }
                   }
                   this.reportResult = this.reportResult + '\xa0\xa0\xa0\xa0\xa0\xa0\xa0' + title + "：" + reportResult + "      ";
               }
               this.reportResult = this.reportResult +'\xa0\xa0\xa0\xa0\xa0\xa0\xa0' +"最终结论："+ finalReportResult;
            },

            downloadResultAttach(id) {
                window.open(`${process.env.API_URL}task/downloadResultAttach?resultId=${id}`);
            },
            cancelRemove() {
                return false;
            },
            auditReport() {
                if (!this.audit.result && this.audit.result != 0) {
                    this.$alert('请选择审批结果', '提示', {
                        type: 'error'
                    })
                    return;
                }
                if (this.audit.result === 0 && this.audit.reason === '') {
                    this.$alert('请输入拒绝原因', '提示', {
                        type: 'error'
                    })
                    return;
                }
                if (this.form.taskStatus == 6) {
                    postReportAuditService(this.form.id, this.audit.result, this.audit.reason).then(res => {
                        if (res.data.success) {
                            this.$message({
                                message: res.data.message,
                                type: 'success',
                                duration: 1000
                            });
                            this.dialogVisible = false;
                            this.$emit('refreshAdd');
                        } else {
                            this.$alert(res.data.message, "提示", {
                                type: "error"
                            });
                        }
                    });
                } else {
                    //   if(this.audit.isPush === "" && this.form.taskType == 2) {
                    //      this.$alert('是否推送不能为空', '提示', {
                    //     type: 'error'
                    // })
                    // return;
                    // }
                    // if(this.form.taskType != 2) {
                        this.audit.isPush = 0;
                    // } 
                    postReportSecondAuditService(this.form.id, this.audit.result, this.audit.isPush, this.audit.reason).then(res => {
                        if (res.data.success) {
                            this.$message({
                                message: res.data.message,
                                type: 'success',
                                duration: 1000
                            });
                            this.dialogVisible = false;
                            this.$emit('refreshAdd');
                        } else {
                            this.$alert(res.data.message, "提示", {
                                type: "error"
                            });
                        }
                    });
                }

            },
            getData(row, index) {
                if (row.taskField.partField.operator == 'choose') {
                    return getLableFromList(RESULT_LIST, row['actData' + index]);
                } else {
                    return row[`actData${index}`];
                }
            },
            submitReport() {
                postSaveReportService(this.form).then(res => {
                    if (res.data.success) {
                        this.$message({
                            message: res.data.message,
                            type: 'success',
                            duration: 1000
                        });
                        this.dialogVisible = false;
                    } else {
                        this.$alert(res.data.message, "提示", {
                            type: "error"
                        });
                    }
                });
            },
            getPosition(type) {
                return getLableFromList(this.positionList, type);
            },
            handleClose() {
                /* 关闭的时候更新状态值 */
                this.sampleStatusName="";
                this.isFullScreen = false;
                this.dialogVisible = false;
            },
            handleClick(tab, event) {

            },
            submitUpload() {
                this.$refs.upload.submit();
            },
            uploadSuccess(res, file, fileList) {
                console.log(res)
                console.log(file);
                if (res.success) {
                    file.url = `${process.env.API_URL}task/downloadAttach?attachId=${res.data.result[0].id}`;
                    file.id = res.result[0].id;
                    file.percentage = 100;
                    console.log(fileList);
                    this.$message({
                        message: res.data.message,
                        type: 'success',
                        duration: 1000
                    });
                } else {
                    this.$alert(res.message, "提示", {
                        type: "success"
                    });
                }
            },
            handleRemove(file, fileList) {
                debugger;
                // postDeleteAttach(file.id).then(res => {
                //   if (!res.data.success) {
                //     this.$alert(res.data.message, "提示", {
                //       type: "error"
                //     });
                //   }
                // })
                console.log(file, fileList);
                return false;
            },
            handlePreview(file) {
                window.open(file.url);
                console.log(file);
            }
        }
    };

</script>

<style scoped>
    .uploadTitle {
        overflow: hidden;
        border-bottom: 1px solid #e2dede;
        margin-bottom: 10px;
    }

</style>
